package com.android.lucky

import android.animation.Animator
import android.animation.AnimatorListenerAdapter
import android.animation.ValueAnimator
import android.os.Bundle
import android.widget.Toast
import androidx.appcompat.app.AppCompatActivity
import androidx.constraintlayout.widget.ConstraintLayout
import com.android.lucky.adapter.LuckyAdapter
import com.android.lucky.util.DataSource
import com.android.lucky.util.DisplayUtil
import kotlinx.android.synthetic.main.activity_main.*

class MainActivity : AppCompatActivity() {

    val TAG = "MainActivity"
    var adapter: LuckyAdapter? = null

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)
        initView()
    }

    private fun initView() {
        recyclerViewLucky.post {
            val rvWidth = recyclerViewLucky.width
            adapter = LuckyAdapter(rvWidth)
            recyclerViewLucky.adapter = adapter
            adapter!!.setList(DataSource().data())

            val lp = recyclerViewLucky.layoutParams as ConstraintLayout.LayoutParams
            lp.height = rvWidth / 5 * 3 + DisplayUtil.dip2px(this, 18f)
            recyclerViewLucky.requestLayout()

            adapter!!.setOnItemClickListener { _, _, position ->
                if (position == 4) {
                    startAnim(8)
                }
            }

            val centerLp = layoutStart.layoutParams as ConstraintLayout.LayoutParams
            centerLp.width = (rvWidth - DisplayUtil.dip2px(this,18f)) / 3
            centerLp.height = (rvWidth / 5 * 3) / 3
            layoutStart.requestLayout()

        }
    }

    private var mShouldStartNextTurn = true // 标记是否应该开启下一轮抽奖
    private var mStartLuckPosition = 0 // 开始抽奖的位置
    private var mCurrentPosition = -1 // 当前转圈所在的位置
    private val mRepeatCount = 8 // 转的圈数
    private val mLuckAnimationEndListener: OnLuckAnimationEndListener? = null

    private fun startAnim(mLuckNum: Int) {
        if (!mShouldStartNextTurn) {
            return
        }
        val animator = ValueAnimator.ofInt(mStartLuckPosition, mRepeatCount * 9 + mLuckNum)
                .setDuration(6000)
        animator.addUpdateListener { animation ->
            val position = animation.animatedValue as Int
            setCurrentPosition(position % 8)
            mShouldStartNextTurn = false
        }
        animator.addListener(object : AnimatorListenerAdapter() {
            override fun onAnimationEnd(animation: Animator) {
                mShouldStartNextTurn = true
                mStartLuckPosition = mLuckNum
                mLuckAnimationEndListener?.onLuckAnimationEnd(
                        mCurrentPosition,
                        adapter!!.data[mCurrentPosition].value
                )
                Toast.makeText(this@MainActivity, "恭喜:$mCurrentPosition" + adapter!!.data[adapter!!.getPos(mCurrentPosition)].value, Toast.LENGTH_SHORT).show()
            }
        })
        animator.start()
    }

    private fun setCurrentPosition(position: Int) {
        mCurrentPosition = position
        adapter!!.setItemSelectBg(position)
    }

    /**
     * 用于抽奖结果回调
     */
    interface OnLuckAnimationEndListener {
        fun onLuckAnimationEnd(pos: Int, msg: String?)
        fun onStart()
    }
}